import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MultiImageComposer {

    // 图片层类
    static class ImageLayer {
        private BufferedImage image;
        private int x, y;
        private float opacity;  // 透明度 (0.0-1.0)

        public ImageLayer(BufferedImage image, int x, int y, float opacity) {
            this.image = image;
            this.x = x;
            this.y = y;
            this.opacity = opacity;
        }
    }

    public static void main(String[] args) {
        try {
            // 创建图层列表
            List<ImageLayer> layers = new ArrayList<>();

            // 添加背景图层
            BufferedImage background = ImageIO.read(new File("D:\\img\\ddd.png"));
            layers.add(new ImageLayer(background, 0, 0, 1.0f));

            // 添加前景图层1
            BufferedImage foreground1 = ImageIO.read(new File("D:\\img\\gh_82c77f85fadd_860.jpg"));

            int targetWidth1 = 120;
            int targetHeight1 = 120;

            BufferedImage resizedImage1 = resizeImage(foreground1, targetWidth1, targetHeight1);
            layers.add(new ImageLayer(resizedImage1, 50, 50, 1.0f)); // 可调整为 1.0f 完全不透明

            // 添加前景图层2
            BufferedImage foreground2 = ImageIO.read(new File("D:\\img\\head.jpeg"));
            int targetWidth2 = 100;
            int targetHeight2 = 100;

            BufferedImage resizedImage2 = resizeImage(foreground2, targetWidth2, targetHeight2);
            layers.add(new ImageLayer(resizedImage2, 200, 100, 1.0f)); // 可调整为 1.0f 完全不透明

            // 合成图片
            BufferedImage result = composeImages(layers);

            // 保存结果
            ImageIO.write(result, "PNG", new File("D:\\img\\multi\\composite.png"));

            System.out.println("多张图片合成完成！");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 高质量缩放图片
     */
    private static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, originalImage.getType());
        Graphics2D g2d = resizedImage.createGraphics();

        // 设置高质量渲染参数
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        // 缩放绘制
        g2d.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
        g2d.dispose();

        return resizedImage;
    }

    // 多张图片合成方法
    private static BufferedImage composeImages(List<ImageLayer> layers) {
        if (layers.isEmpty()) {
            return null;
        }

        // 获取第一张图片作为基准尺寸
        BufferedImage firstImage = layers.get(0).image;
        int width = firstImage.getWidth();
        int height = firstImage.getHeight();

        // 创建结果图片
        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = result.createGraphics();

        // 设置抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制每一层
        for (ImageLayer layer : layers) {
            // 设置透明度
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, layer.opacity));
            g2d.drawImage(layer.image, layer.x, layer.y, null);
        }

        g2d.dispose();
        return result;
    }
}
